import 'package:flutter/material.dart';

class BadgeIcon extends StatelessWidget {
  final Widget icon; // 菜单图标
  final int count; // 显示的数字
  final double size; // 徽章大小
  final Color badgeColor; // 徽章颜色
  final Color textColor; // 文字颜色

  const BadgeIcon({
    super.key,
    required this.icon,
    required this.count,
    this.size = 16.0,
    this.badgeColor = Colors.white,
    this.textColor = Colors.red,
  });

  @override
  Widget build(BuildContext context) {
    // 当数字为0时不显示徽章
    if (count <= 0) return icon;

    final displayText = count > 99 ? '99+' : '$count';
    final fontSize = size * (displayText.length > 2 ? 0.6 : 0.7);

    return Stack(
      clipBehavior: Clip.none, // 允许徽章超出边界
      children: [
        // 原始菜单图标
        icon,

        // 数字徽章
        Positioned(
          top: -size / 3, // 部分重叠在图标上
          right: -size / 3,
          child: Container(
            padding: const EdgeInsets.all(2.0),
            constraints: BoxConstraints(
              minWidth: size,
              minHeight: size,
            ),
            decoration: BoxDecoration(
              color: badgeColor,
              shape: BoxShape.circle,
              border: Border.all(
                color: Theme.of(context).canvasColor, // 使用背景色作为边框
                width: 1.5,
              ),
              boxShadow: [
                BoxShadow(
                  color: Colors.black.withOpacity(0.2),
                  blurRadius: 2.0,
                  offset: const Offset(0, 1),
                )
              ],
            ),
            child: Center(
              child: Text(
                displayText,
                style: TextStyle(
                  color: textColor,
                  fontSize: fontSize,
                  fontWeight: FontWeight.bold,
                ),
                textAlign: TextAlign.center,
              ),
            ),
          ),
        ),
      ],
    );
  }
}
